<template>
  <div class="supply">
    <el-card class="box-card">
      <div slot="header" class="card-title clearfix">
        <span>商品上新</span>
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="供应商名" prop="supplyName">
          <el-input type="text" v-model="ruleForm.supplierName" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplyId">
          <el-input type="text" v-model="ruleForm.supplierId" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品名" prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off" placeholder="请输入商品名"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input type="text" v-model="ruleForm.description" autocomplete="off" placeholder="请输入商品描述"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input type="text" v-model="ruleForm.price" autocomplete="off" placeholder="请输入商品价格"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" prop="price">
          <el-input type="text" v-model="ruleForm.stock" autocomplete="off" placeholder="请输入商品库存"></el-input>
        </el-form-item>
        <el-form-item label="商品类型ID" prop="pcId">
          <el-input type="text" v-model="ruleForm.pcId" autocomplete="off" placeholder="请输入商品类型ID"></el-input>
        </el-form-item>
        <el-form-item label="商品品牌ID" prop="pcId">
          <el-input type="text" v-model="ruleForm.pcChildId" autocomplete="off" placeholder="请输入商品品牌ID"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="imgPath">
          <el-input type="text" v-model="ruleForm.imgPath" autocomplete="off" placeholder="请输入商品图片URL"></el-input>
        </el-form-item>
        <el-form-item label="商品图片预览" prop="imgPath">
          <el-card class="box-card" shadow="always" style="width: 240px">
            <el-image
              style="display: flex; justify-content: center;"
              :src="ruleForm.imgPath"
              fit="fill">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </el-card>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加商品</el-button>
          <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="warning" @click="$router.push('/')">返回首页</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: "Supply",
  created() {
    this.supplier = this.$store.getters.getUser;
    this.ruleForm.supplierName = this.supplier.username;
    this.ruleForm.supplierId = this.supplier.id;
  },
  data() {
    return {
      supplier: null,
      ruleForm: {
        supplierName: '',
        supplierId: '',
        name: '',
        description: '',
        price: null,
        stock: null,
        pcId: '',
        pcChildId: '',
        imgPath: '',
      },
      rules: {}
    };
  },
  methods: {
    submitForm(formName) {
      let productForm = {
        supplierId: this.ruleForm.supplierId,
        name: this.ruleForm.name,
        description: this.ruleForm.description,
        price: this.ruleForm.price,
        stock: this.ruleForm.stock,
        pcId: this.ruleForm.pcId,
        pcChildId: this.ruleForm.pcChildId,
        imgPath: this.ruleForm.imgPath
      };

      this.$axios.post("/product/add", productForm).then(res => {
        this.$router.push("/");
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.supply {
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.card-title {
  display: flex;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
}
</style>